# load libraries
library(dplyr)
library(readxl)
library(janitor)
library(ggplot2)
library(skimr)
library(stringr)
library(maps)
library(mapdata)
library(plotly)
library(sf)
library(ggthemes)
library(ggpubr)

Sys.setenv('MAPBOX_TOKEN' = 'pk.eyJ1IjoiYXJpa2IwIiwiYSI6ImNrM2V5djl2cDAwbzkzZG5sMG16ZndnbmYifQ.BfoEKQZ2gykEJvFmt1xe8Q')

Description

Headline

The following plot….

listings <- read.csv("data/listings-summarized.csv")
neighbourhoods <- read.csv("data/neighbourhoods.csv")
all_listings <-read.csv("data/listings.csv")

ottawawards <- read_sf("data/Wards.shp")

ditch_the_axes <- theme(
  axis.text = element_blank(),
  axis.line = element_blank(),
  axis.ticks = element_blank(),
  panel.border = element_blank(),
  panel.grid = element_blank(),
  axis.title = element_blank(),
  panel.background = element_rect(fill = "white", colour = "white")
)

ottawawards$WARD_NAME_[ottawawards$WARD_NAME_ == "Orléans"] <- "Orleans"
ottawawards$WARD_NAME_[ottawawards$WARD_NAME_ == "West Carleton – March"] <- "West Carleton-March"
ottawawards$WARD_NAME_[ottawawards$WARD_NAME_ == "Stittsville"] <- "Stittsville-Kanata West"

ottawawards <- ottawawards %>%
  left_join(count(listings,neighbourhood),c("WARD_NAME_"="neighbourhood"))

ottawawards %>% ggplot() + 
  geom_sf(aes(fill=n),color = "white") +
  scale_fill_gradient(low = "light blue", high = "dark blue") +
  ditch_the_axes

df_listing_sum <- listings %>% 
  group_by(neighbourhood, room_type) %>% 
  summarize(sum = n()) %>%
  group_by(neighbourhood) %>%
  mutate(total=sum(sum)) %>%
  mutate(percent=total/nrow(listings))
  
df_listing_sum %>%
  ggplot(aes(x=reorder(neighbourhood,total),y=sum,fill=room_type)) +
  geom_bar(stat="identity") +
  coord_flip() +
  theme_minimal()

p <- listings %>%
  plot_mapbox(lat = ~latitude, lon = ~longitude,
              split = ~room_type, size=2,
              mode = 'scattermapbox', hoverinfo="price") %>%
              layout(mapbox = list(zoom = 9,
                       center = list(lat = ~median(latitude),
                                     lon = ~median(longitude))))
p
cal <- read.csv("data/calendar.csv") %>%
  filter(as.Date(date) < as.Date("2019-11-01")) %>%
  filter(available =="f")

reviews <- read.csv("data/reviews.csv") %>%
  filter(as.Date(date) > as.Date("2019-01-01"))

rev <- reviews %>% count(listing_id)

#getting rid of not active listings, i.e havent been reviewd since hte beging of the year and have less than 1 review per month
df_activity <- listings %>% filter(number_of_reviews != 0) %>%
  filter(as.Date(last_review) > as.Date("2018-12-31")) %>%
  select(id,neighbourhood,minimum_nights,price,reviews_per_month,number_of_reviews,room_type) %>% 
  mutate(
    min_price_per_month = if_else(
      #((minimum_nights > 30) & (reviews_per_month > 0.8)),
      minimum_nights * reviews_per_month > 30,
      price * minimum_nights / 30,
      reviews_per_month * price * minimum_nights
      )
    )

df_minimum_activity <- df_activity %>%
  group_by(neighbourhood,room_type) %>%
  summarise(average=mean(min_price_per_month))

df_minimum_activity %>% ggplot(aes(x=reorder(neighbourhood,average),y=average,fill=room_type)) +
  geom_bar(stat="identity",position = "dodge") +
  coord_flip() +
  theme_minimal()

generateGraphMinPrice <- function(name,colour) {
  df_minimum_activity %>% filter(room_type==name) %>%
    ggplot(aes(x=reorder(neighbourhood,average),y=average,fill=room_type)) +
    geom_bar(stat="identity",position = "dodge",fill=colour) +
    coord_flip() +
    theme_minimal()
}

a <- generateGraphMinPrice("Entire home/apt","red")
b <- generateGraphMinPrice("Hotel room","green")
c <- generateGraphMinPrice("Private room","cyan")
d <- generateGraphMinPrice("Shared room","blue")
#df_amount_listings <- listings %>% distinct(host_id,calculated_host_listings_count)%>% filter(calculated_host_listings_count > 2)
ggarrange(a,b,c,d+rremove("x.text"),ncol =2,nrow = 2,labels=c("Entire home/apt","Hotel room","Private room","Shared room"),common.legend = TRUE,legend = "bottom")

Explanation of the plot…

Comments

Suggestions for further explorations

References

Appendix: sessionInfo

sessionInfo()
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Mojave 10.14.3
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] ggpubr_0.2.3   magrittr_1.5   ggthemes_4.2.0 sf_0.8-0      
##  [5] plotly_4.9.1   mapdata_2.3.0  maps_3.3.0     stringr_1.4.0 
##  [9] skimr_1.0.7    ggplot2_3.2.1  janitor_1.2.0  readxl_1.3.1  
## [13] dplyr_0.8.3   
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_0.2.5   xfun_0.9           purrr_0.3.2       
##  [4] colorspace_1.4-1   vctrs_0.2.0        htmltools_0.4.0   
##  [7] viridisLite_0.3.0  yaml_2.2.0         rlang_0.4.0       
## [10] later_1.0.0        e1071_1.7-2        pillar_1.4.2      
## [13] glue_1.3.1         withr_2.1.2        DBI_1.0.0         
## [16] lifecycle_0.1.0    munsell_0.5.0      ggsignif_0.6.0    
## [19] gtable_0.3.0       cellranger_1.1.0   htmlwidgets_1.5.1 
## [22] evaluate_0.14      labeling_0.3       knitr_1.24        
## [25] fastmap_1.0.1      httpuv_1.5.2       crosstalk_1.0.0   
## [28] class_7.3-15       Rcpp_1.0.2         xtable_1.8-4      
## [31] KernSmooth_2.23-15 promises_1.1.0     scales_1.0.0      
## [34] backports_1.1.4    classInt_0.4-2     jsonlite_1.6      
## [37] mime_0.7           digest_0.6.20      stringi_1.4.3     
## [40] shiny_1.4.0        cowplot_1.0.0      grid_3.6.1        
## [43] tools_3.6.1        lazyeval_0.2.2     tibble_2.1.3      
## [46] crayon_1.3.4       tidyr_1.0.0        pkgconfig_2.0.2   
## [49] zeallot_0.1.0      data.table_1.12.6  assertthat_0.2.1  
## [52] rmarkdown_1.15     httr_1.4.1         R6_2.4.0          
## [55] units_0.6-5        compiler_3.6.1